package com.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.domain.IssueCommentResponse;
import com.domain.IssueCommentResponseGrandson;
import com.domainVO.PageInfoVO;
import com.mapper.helping.IssueCommentResponseGrandsonMapper;
import com.mapper.helping.IssueCommentResponseMapper;
import com.mapper.helping.IssueMapper;
import com.mapper.living.UserDao;
import com.service.IIssueCommentResponseGrandsonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.util.UserUtil;
import living.com.domain.User;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 大忽悠
 * @since 2022年02月12日
 */
@RequiredArgsConstructor
@Service
public class IssueCommentResponseGrandsonServiceImpl extends ServiceImpl<IssueCommentResponseGrandsonMapper, IssueCommentResponseGrandson> implements IIssueCommentResponseGrandsonService {
     private final IssueCommentResponseGrandsonMapper issueCommentResponseGrandsonMapper;
     private final IssueCommentResponseMapper issueCommentResponseMapper;
     private final UserDao userDao;
     private final IssueMapper issueMapper;
    /**
     * 只有版主和和当前评论者自己可以删除
     * 孙子评论---儿子评论id----回答id---回答对应的用户
     * 孙子评论对应的用户
     */
    @Override
    public Boolean logicDelById(Integer grantSon_comment_id) {
        //只有发布回答的版主和自己才可以删除评论
        IssueCommentResponseGrandson issueCommentResponseGrandson =
                issueCommentResponseGrandsonMapper.selectById(grantSon_comment_id);
        //儿子评论id
        Integer fatherCommentId = issueCommentResponseGrandson.getFatherCommentId();
        //查询版主对应的用户id
        Integer fatherCommentId1 = issueCommentResponseMapper.selectById(fatherCommentId).getFatherCommentId();
        Integer toUserId = issueMapper.selectById(fatherCommentId1).getUserId();
        Assert.isTrue(toUserId.equals(UserUtil.getUserId())||
                //孙子评论的发布者
                issueCommentResponseGrandson.getFromUserId().equals(UserUtil.getUserId()),"无权删除当前评论");
        return issueCommentResponseGrandsonMapper.logicDelById(grantSon_comment_id);
    }

    @Override
    public Boolean WeiXinDel(Integer grantSon_comment_id) {
        return issueCommentResponseGrandsonMapper.logicDelById(grantSon_comment_id);
    }

    /**
     * 传入子评论集合
     * @param records
     * @return
     */
    public List<IssueCommentResponseGrandson> commonOperation(List<IssueCommentResponseGrandson> records)
    {
        //每个子评论带当前用户信息
        List<IssueCommentResponseGrandson> grantSonCommentUserVOS = records.stream().map(grantSon -> {
            //如果当前子评论不是匿名版主发布的
            if(grantSon.getFromUserId()!=0)
            {
                 User byId = userDao.publishWithAttention(grantSon.getFromUserId(),UserUtil.getUserId());
//                grantSon.setPublisher(byId);
                grantSon.setFromUsername(byId.getUsername());
                grantSon.setFromUserAvatar(byId.getAvatar());
            }
            //区分回复对象
            if(grantSon.getToUserId()==0)
            {
                 //回复的是儿子评论
                 //判断儿子评论是否是匿名版主发布的
                IssueCommentResponse issueCommentResponse = issueCommentResponseMapper.selectById(grantSon.getFatherCommentId());
                //是匿名版主发布的,就将用户名设置为null
                //否则设置用户名
                if(!(issueCommentResponse.getFromUserId()==0&&issueCommentResponse.getToUserId()==0))
                {
                    User toUser = userDao.getUserById(issueCommentResponse.getToUserId());
                    String username = toUser.getUsername();
                    grantSon.setToUsername(username);
                }
            }
            else if(grantSon.getToUserId()==-1)
            {
                //回复的是同级匿名版主的孙子评论--不带用户名

            }else
            {
                //回复的是其他孙子评论,而且不是匿名版主发布的
                User userById = userDao.getUserById(grantSon.getToUserId());
                grantSon.setToUsername(userById.getUsername());
            }
            //孙子评论被删除了
            if(grantSon.getIsDeleted()==1)
            {
                grantSon.setContent(null);
            }
            return grantSon;
        }).collect(Collectors.toList());
        return grantSonCommentUserVOS;
    }

    @Override
    public Map<String, Object> getSonCommentUnderTargetFatherComment(Integer father_comment_id, Integer startPage, Integer pageSize) {
        Page<IssueCommentResponseGrandson> page=new Page<>(startPage,pageSize);
        Page<IssueCommentResponseGrandson> responsePage = issueCommentResponseGrandsonMapper.selectPage(page, new QueryWrapper<IssueCommentResponseGrandson>()
                .eq("father_comment_id", father_comment_id)
                .orderByDesc("id"));
        List<IssueCommentResponseGrandson> grantSonCommentUserVOS = commonOperation(responsePage.getRecords());
        return PageInfoVO.builder().autoWired(page,grantSonCommentUserVOS);
    }
}
